#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "readwave.h"
#include "mfcc.h"

void main1(int argc, char **argv)
{

#ifdef _DEBUG
	_CrtSetBreakAlloc(0);
#endif


	int i;
	
	char filename[256];
	strcpy(filename, argv[1]);
	
	int numSamples, nSmpRate;
	WavHead wavhead;
	short *WaveData = ReadWave(filename, &numSamples, &wavhead);
	nSmpRate = wavhead.SampleRate;
	

	FEATURE_MFCCBASEINFO p_info;
	MFCC *stat = new MFCC;
	stat->Initialize(p_info);
	
	StatResult result;
	result = stat->ComputeStat(WaveData,numSamples/10,nSmpRate);
	
	FILE *fp = fopen("StatSpect.txt","w");

	fprintf(fp,"mean\t\tmedian\t\tvar\t\tskew\t\tkurto\t\tmin\t\tmax\n");

	for (i=0; i<result.chlNum; i++)
	{
		fprintf(fp,"%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n",result.mean[i], result.median[i], result.var[i], result.skew[i], result.kurto[i], result.minV[i], result.maxV[i]);
		fprintf(fp,"\n");
	}
	
	fclose(fp);

	int nFrameShift = p_info.framePeriod * p_info.smpRate/1e3 ;
	int nWinSize = p_info.winSize * p_info.smpRate/1e3;
	int numFrame = (numSamples-nWinSize)/nFrameShift+1;
	
	FILE *fp2 = fopen("result.txt", "w");
	short *pCurWave=WaveData;
	for (i=0; i<numFrame; i++)
	{
		float fspread = stat->MelBandSpread(pCurWave,nWinSize,nSmpRate);
		
		fprintf(fp2,"%.2f\n",fspread);
		pCurWave += nFrameShift;
	}
	
	fclose(fp2);
	
	delete []WaveData;
	delete stat;
	delete []result.mean;
	delete []result.median;
	delete []result.var;
	delete []result.kurto;
	delete []result.skew;
	delete []result.maxV;
	delete []result.minV;

#ifdef _DEBUG
	_CrtDumpMemoryLeaks();
#endif


}

void main(int argc, char **argv)
{

#ifdef _DEBUG
//	_CrtSetBreakAlloc(0);
#endif


	int i,j;
	
	char filename[256];
	strcpy(filename, argv[1]);
	
	int numSamples, nSmpRate;
	WavHead wavhead;
	short *WaveData = ReadWave(filename, &numSamples, &wavhead);
	nSmpRate = wavhead.SampleRate;
	
	
	FEATURE_MFCCBASEINFO p_info;
	MFCC *MFCCtrain = new MFCC;
	p_info.normEnergy=true;
	strcpy(p_info.targetKind,"MFCC");
	MFCCtrain->Initialize(p_info);
	
	int RunBufSize;
	MFCCtrain->GetRunBufSize(numSamples,RunBufSize);
	char* MFCC_RunBuf=(char *)malloc(sizeof(float)*RunBufSize);
	int numFrames;
	MFCCtrain->ComputeMFCC(WaveData,numSamples,MFCC_RunBuf,numFrames);
	
	float *MFCC_FeatBuf;
	MFCC_FeatBuf=(float *)(MFCC_RunBuf+MFCCtrain->GetOffSet());
	
	FILE *fp = fopen("mfcc.txt","w");
	
	for (i=0; i<numFrames; i++)
	{
		for (j=0; j<MFCCtrain->m_nFeatDim; j++)
		{
			fprintf(fp,"%.2f\t", (float)MFCC_FeatBuf[i*MFCCtrain->m_nFeatDim+j]);
		}
		fprintf(fp,"\n");
	}
	
	fclose(fp);

	float *SDC_FeatBuf;
	SDC_FeatBuf = MFCCtrain->ComputeSDC(MFCC_FeatBuf,numFrames);

	FILE *fp2 = fopen("SDC.txt","w");
	
	for (i=0; i<numFrames/10; i++)
	{
		for (j=0; j<MFCCtrain->m_nFeatDim_SDC; j++)
		{
			fprintf(fp2,"%.2f\t", (float)MFCC_FeatBuf[i*MFCCtrain->m_nFeatDim+j]);
		}
		fprintf(fp2,"\n");
	}
	
	fclose(fp2);


	delete []WaveData;
	delete []MFCC_RunBuf;
	delete []SDC_FeatBuf; 
	delete MFCCtrain;

#ifdef _DEBUG
	_CrtDumpMemoryLeaks();
#endif
}